<?php
	/**
	* Searches haystack for needle and 
	* returns an array of the key path if 
	* it is found in the (multidimensional) 
	* array, FALSE otherwise.
	*
	* @mixed array_searchRecursive ( mixed needle, 
	* array haystack [, bool strict[, array path]] )
	*/ 
	function array_searchRecursive( $needle, $haystack, $strict=false, $path=array() )
	{
		if( !is_array($haystack) ) {
			return false;
		}
	 
		foreach( $haystack as $key => $val ) {
			if( is_array($val) && $subPath = array_searchRecursive($needle, $val, $strict, $path) ) {
				$path = array_merge($path, array($key), $subPath);
				return $path;
			} elseif( (!$strict && $val == $needle) || ($strict && $val === $needle) ) {
				$path[] = $key;
				return $path;
			}
		}
		return false;
	}
	//uses('view/helpers/Form');
	class TextDateHelper extends AppHelper{
		var $helpers = array('Html', 'Form');		
		var $_defaults = array(
			'type'=>'text',
			'id'=>'',
			'name'=>'', //name del textbox siempre irá en blanco para no enviar su valor, pero si asumen el nombre los controles hidden
			//'size'=>'8',
			'maxlength'=>'10',
			'class'=>'',
			'div'=>true,
			'label'=>false,
			'value'=>'00/00/0000', //puede ser un array de tipo fecha CakePHP
			'before'=>array(),
			'enablecero'=>true, // habilitar valor 00/00/0000
			'button'=>true
		);
		
		/*
		* Descripcion: 
		* 	crear el input compatible con el control $form->input(... 'type'=>'date') 
		* 	utilizando caja de texto y controles ocultos. La validación del formato de fecha es por javascript.
		*	No requiere conversión de tipo del lado del servidor
		* Parametros:
		* 	*(String)$name -> nombre del control
		* 	*(Array)$options -> lista de opciones del input de tipo text
		Uso:
		*	//controller:
		*		$helpers = array('TextDate');
		*	//view:
		*		<?php echo $javascript->link(array('add-edit.only'), true); ?>
		*		echo $textDate->input('Viatico.fecha',array('value'=>$session->read('Ejercicio.Actual.fecha')));
		*/
		function input($name, $options=array()){
			$options = array_merge($this->_defaults, $options);
			if(empty($options['id'])){	
				$_id = $this->domId($name); 
			} else {
				$_id = $options['id']; 
			}
			$_name = $this->__name($name);
			
			if(!empty($this->data) && (empty($options['value'])||($options['value']=='00/00/0000'))){
				$_test = '$this->'.str_replace('[', '["', str_replace(']', '"]', $_name));
				eval("if (isset($_test)){\$test = $_test;}");
				if(!empty($test)){
					$options['value']=$test;
				}
			}
			
			if(!$options['enablecero'] && (empty($options['value'])||($options['value']=='00/00/0000'))){				
				//$options['value'] = date('d/m/Y');	
				$options['value'] = $_SESSION['Ejercicio']['Actual']['fecha'];
			}			
			if((gettype($options['value'])!= 'array')&&(count(explode('-', $options['value']))>1)){
				$val = implode('/', array_reverse(explode('-', $options['value'])));
				$options['value'] = $val;
			}
			
			if(!empty($options['value'])){
				if(gettype($options['value']) == 'array' && array_key_exists('day', $options['value'])){
					$value[0]=$options['value']['day'];
					$value[1]=$options['value']['month'];
					$value[2]=$options['value']['year'];
					$options['value'] = implode('/', $options['value']);
				}elseif(gettype($options['value']) == 'string'){
					$value = explode('/', $options['value']);
				}else{
					$value = array('', '', '');
					$options['value']='';
				}
			} else {
				$value = array('', '', '');
			}
			
			$hiddenDate = 
				$this->Form->input($_name.'.day', array('name'=>$_name.'[day]','type'=>'hidden', 'value'=>$value[0], 'id'=>$_id.'Day')).
				$this->Form->input($_name.'.month', array('name'=>$_name.'[month]','type'=>'hidden', 'value'=>$value[1], 'id'=>$_id.'Month')).
				$this->Form->input($_name.'.year', array('name'=>$_name.'[year]','type'=>'hidden', 'value'=>$value[2], 'id'=>$_id.'Year'));
			
			if(empty($options['before'])){
				$options['before'] = $hiddenDate;
			} else {
				$options['before'] .= $hiddenDate;
			}
			
			$class = 'TextDateHelper';
			if($options['button']){
				$class .= ' dateselector';
			}
			if(!empty($options['class'])){
				$options['class'] .= " $class";
			} else {
				$options['class'] = $class;
			}
			//if(empty($otions['onchange'])){
//				$options['onchange'] = 'TextDateHelperFn(e={type:"change"},this)';
//			} else {
//				$options['onchange'] = 'TextDateHelperFn(e={type:"change"},this);'.$options['onchange'];
//			}
			
			$options['name']='';
			$options['id']=$_id;
			
			return $this->Form->input($name, $options);
		}
		
		
	}
?>